<?php


namespace app\api\controller\keyan;


use app\common\controller\Api;
use app\common\model\File;
use app\common\model\MeetingType;
use think\Db;
use think\exception\PDOException;
use think\exception\ValidateException;

/**
 * 会议信息
 */
class Meeting extends Api
{
    /**
     * 列表
     */
    public function list()
    {
        $model = new \app\common\model\Meeting();
        $pageNum = input('pageNum', 1);
        $pageSize = input('pageSize', 10);
        $name = input('name');
        $title = input('title');
        $type_id = input('type_id');
        $where_sql = [];
        $where_sql['user_id'] = $this->auth->id;
        if ($name) {
            $where_sql['name'] = ['like', "%$name%"];
        }
        if ($title) {
            $where_sql['title'] = ['like', "%$title%"];
        }
        if ($type_id) {
            $where_sql['type_id'] = $type_id;
        }
        $data = $model->where($where_sql)
            ->order(['top'=>'desc', 'id'=>'desc'])
            ->paginate(['page'=>$pageNum,'list_rows'=>$pageSize]);
        $this->success('', $data);
    }

    /**
     * 会议参办类型
     */
    public function type_list()
    {
//        $type = input('type', 1);
//        if ($type == 1) {
//            $model = new AwardLevel();
//        }  else {
//            $model = new AwardType();
//        }
        $model = new MeetingType();

        $list = $model->select();
        $this->success('', $list);
    }

    /**
     * 添加
     *
     * @ApiMethod (POST)
     */
    public function add()
    {
        $param = input('post.');
        if (!$param) {
            $this->error('参数不正确');
        }
        $user_id = $this->auth->id;
        $param['user_id'] = $user_id;
        Db::startTrans();
        try {
            $dataModel = new \app\common\model\Meeting();
            $dataModel->data($param);
            $dataModel->allowField(true)->save();
            $data_id = $dataModel->id;

            //附件
            if (isset($param['file']) && $param['file']) {
                File::where('id', 'in', $param['file'])->update(['pid'=>$data_id,'type'=>5,'user_id'=>$user_id]);
            }
            Db::commit();
        } catch (ValidateException|PDOException|\Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success('保存成功');

    }

    /**
     * 单个会议信息
     */
    public function info()
    {
        $id = input('id');
        if (!$id) {
            $this->error('id不能为空');
        }
        $model = new \app\common\model\Meeting();
        $where_sql['id'] = $id;
        $user_id = $this->auth->id;
        $where_sql['user_id'] = $user_id;
        $info = $model->where($where_sql)->find();
        if ($info) {
            $info['file'] = File::where(['pid'=>$id,'type'=>5,'user_id'=>$user_id])->field('id,name,url')->select();
        }
        $this->success('', $info);
    }

    /**
     * 编辑
     *
     * @ApiMethod (POST)
     */
    public function edit()
    {
        $dataModel = new \app\common\model\Meeting();
        $param = input('post.');
        $user_id = $this->auth->id;
        Db::startTrans();
        try {
            $dataModel->allowField(true)->isUpdate(true)->save($param);

            //附件
            if (isset($param['file']) && $param['file']) {
                File::where('id', 'in', $param['file'])->where(['user_id'=>$user_id])->update(['pid'=>$param['id'],'type'=>5]);
                File::where('id', 'not in', $param['file'])->where(['pid'=>$param['id'],'user_id'=>$user_id,'type'=>5])->delete();
            }
            Db::commit();
        } catch (ValidateException|PDOException|\Exception $e) {
            Db::rollback();
            $this->error('修改失败,请重试');
        }

        $this->success('修改成功');
    }
    /**
     * 删除
     *
     * @ApiMethod (POST)
     */
    public function del()
    {
        $id = $this->request->post('id/a');
        if (!$id) {
            $this->error('id不能为空');
        }
        $model = new \app\common\model\Meeting();
        $where_sql['id'] = ['in', $id];
        $user_id = $this->auth->id;
        $where_sql['user_id'] = $user_id;
        $model->where($where_sql)->delete();
        File::where(['pid'=>['in',$id],'user_id'=>$user_id,'type'=>5])->delete();
        $this->success('删除成功');
    }

    /**
     * 设置/取消 代表作
     *
     * @ApiMethod (POST)
     * @param int $id 项目id
     * @param int $type 类型,1=设置,其它的=取消代表作
     */
    public function setTop()
    {
        $id = input('id');
        if (!$id) {
            $this->error('id不能为空');
        }
        $model = new \app\common\model\Meeting();
        $where_sql['id'] = $id;
        $where_sql['user_id'] = $this->auth->id;
        $type = input('type', 1);
        if ($type == 1) {
            $result = $model->where($where_sql)->update(['top'=>1]);
        } else {
            $result = $model->where($where_sql)->update(['top'=>0]);
        }

        if ($result !== false) {
            $this->success('设置成功');
        } else {
            $this->error('设置失败');
        }
    }
}